
# coding=utf-8
"""
author: jamon
"""

from package.static.bullet import data as bullet_data
from package.static.gate import data as gate_info
from package.static.skill import data as skill_info
from package.static.tank import data as tank_data
from package.static.type_info import data as type_data
from package.static.const import TypeIdConst, GroupIdConst, Faction
from typing import List, Dict, Optional, Tuple

_need_reload = True


class TypeItem(object):

    def __init__(self, type_id: TypeIdConst):
        _data = type_data.get(type_id)
        self.name: str = _data["name"]
        self.max_speed: int = _data["max_speed"]
        self.max_blood: int = _data["max_blood"]
        self.recovery: int = _data["recovery"]
        self.immune_hurt: int = _data["immune_hurt"]
        self.is_virtual: int = _data["is_virtual"]
        self.tech_level: int = _data["tech_level"]
        self.width: int = _data.get("width", 0)
        self.height: int = _data.get("height", 0)
        self.image: Dict[Faction, list] = _data["image"]  # 不同阵营图标路径
        self.extra: dict = _data.get("extra", {})   # 额外的参数


class BulletDogma(object):

    def __init__(self, type_id: TypeIdConst):
        self.hurt = bullet_data["bullet"][type_id]["hurt"]


class NpcWaveItem(object):
    """每波怪配置"""
    def __init__(self, info: dict):
        self.tank: Dict[TypeIdConst, int] = info["tank"]
        self.skill: List[TypeIdConst] = info["skill"]


class GateItem(object):
    def __init__(self, gate_id: int):
        self.waves: int = len(gate_info[gate_id]["npc"])
        self.npc: List[NpcWaveItem] = [NpcWaveItem(info) for info in gate_info[gate_id]["npc"]]
        self.interval: int = gate_info[gate_id]["interval"]
        self.path: str = gate_info[gate_id]["path"]
        self.width: int = gate_info[gate_id]["width"]
        self.height: int = gate_info[gate_id]["height"]
        self.npc_speed_buff: float = gate_info[gate_id]["npc_speed_buff"]
        self.our_tank_birth: List[Tuple[int, int]] = gate_info[gate_id]["our_tank_birth"]
        self.npc_tank_birth: List[Tuple[int, int]] = gate_info[gate_id]["npc_tank_birth"]
        self.enemy_tank_birth: List[Tuple[int, int]] = gate_info[gate_id]["enemy_tank_birth"]


class SkillDogma(object):

    def __init__(self, type_id: TypeIdConst):
        self.name: str = skill_info[type_id]["name"]
        self.image: str = skill_info[type_id]["image"]
        self.life: int = skill_info[type_id]["life"]
        self.extra: dict = skill_info[type_id]["extra"]


class TankDogma(object):

    def __init__(self, type_id: TypeIdConst):
        self.weapon: TypeIdConst = tank_data[type_id]["weapon"]
        self.dead_image: str = tank_data[type_id]["dead_image"]
        self.hurt_effect: str = tank_data[type_id]["hurt_effect"]


class WeaponDogma(object):

    def __init__(self, type_id: TypeIdConst):
        self.bullet: TypeIdConst = bullet_data["weapon"][type_id]["bullet"]
        self.cool_time: int = bullet_data["weapon"][type_id]["cool_time"]


class Universe(object):

    def __init__(self):
        self._type_info: Dict[TypeIdConst, TypeItem] = {}
        for _id in type_data.keys():
            self._type_info[_id] = TypeItem(_id)

        self._bullet_info: Dict[TypeIdConst, BulletDogma] = {}
        for _id in bullet_data["bullet"].keys():
            self._bullet_info[_id] = BulletDogma(_id)

        self._weapon_info: Dict[TypeIdConst, WeaponDogma] = {}
        for _id in bullet_data["weapon"].keys():
            self._weapon_info[_id] = WeaponDogma(_id)

        self._tank_info: Dict[TypeIdConst, TankDogma] = {}
        for _id in tank_data.keys():
            self._tank_info[_id] = TankDogma(_id)

        self._gate_info: Dict[int, GateItem] = {}
        self.max_gate_id: int = 1
        for gate_id in gate_info.keys():
            self.max_gate_id = max(self.max_gate_id, gate_id)
            self._gate_info[gate_id] = GateItem(gate_id)

        self._skill_info: Dict[TypeIdConst, SkillDogma] = {}
        for _id in skill_info.keys():
            self._skill_info[_id] = SkillDogma(_id)

    @staticmethod
    def get_group_id(type_id: TypeIdConst) -> GroupIdConst:
        return GroupIdConst(type_id // 100000)

    def get_type_base_info(self, type_id: TypeIdConst) -> Optional[TypeItem]:
        return self._type_info.get(type_id)

    def get_bullet_special_info(self, type_id: TypeIdConst) -> Optional[BulletDogma]:
        return self._bullet_info.get(type_id)

    def get_weapon_special_info(self, type_id: TypeIdConst) -> Optional[WeaponDogma]:
        return self._weapon_info.get(type_id)

    def get_tank_special_info(self, type_id: TypeIdConst) -> Optional[TankDogma]:
        return self._tank_info.get(type_id)

    def get_gate_info(self, gate_id: int) -> GateItem:
        return self._gate_info[gate_id]

    def get_skill_info(self, type_id: TypeIdConst) -> Optional[SkillDogma]:
        return self._skill_info[type_id]


class EsException(Exception):

    def __init__(self, name, **kwargs):
        self.name = name
        self.params = kwargs

    def __str__(self):
        return self.__repr__()

    def __repr__(self):
        return "<%s: name=%s params=%s>" % (
            self.__class__.__name__, self.name, self.params)


universe = Universe()
